package com.zsjt.app.service.impl;

import com.zsjt.app.compoent.utils.common.ComUtils;
import com.zsjt.app.mapper.UserDtoMapper;
import com.zsjt.app.pojo.UserDto;
import com.zsjt.app.service.UserDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service("userDtoService")
public class UserDtoServiceImpl implements UserDtoService {

    private UserDtoMapper userDtoMapper;


    @Autowired
    @Qualifier("userDtoMapper")
    public void setUserDtoMapper(UserDtoMapper userDtoMapper) {
        this.userDtoMapper = userDtoMapper;
    }


    /**
     * 更新用户邮箱
     * @param newEmail
     * @return
     */
    @Override
    public boolean updateUserEmail(UserDto user, String newEmail) {
        if(newEmail ==null || newEmail.length()==0){
            newEmail = null;
        }
        user.setEmail(newEmail);
        boolean isOK = false;
        try {
            userDtoMapper.updateUser(user);
            isOK=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return isOK;
    }

    /**
     * 更新用户手机号
     * @param newPhone
     * @return
     */
    @Override
    public boolean updateUserPhone(UserDto user,String newPhone) {
        if(newPhone ==null || newPhone.length()==0){
            newPhone = null;
        }
        user.setPhone(newPhone);
        boolean isOK = false;
        try {
            userDtoMapper.updateUser(user);
            isOK=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return isOK;
    }

    /**
     * 更新用户密码
     * @param user
     * @param newPassword
     * @return
     */
    @Override
    public boolean updateUserPassword(UserDto user, String newPassword) {
        user.setPassword(newPassword);
        boolean isOK = false;
        try {
            userDtoMapper.updateUser(user);
            isOK=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return isOK;
    }

    /**
     * 通过账户查询用户
     * @param userAccount
     * @return
     */
    @Override
    public UserDto findUserByAccount(String userAccount) {
        return userDtoMapper.findUserByAccount(userAccount);
    }


    /**
     *通过id查询用户
     * @param id
     * @return
     */
    @Override
    public UserDto findUserById(Integer id) {
        return userDtoMapper.findUserById(id);
    }

    /**
     * 手机号是否存在
     * @param userPhone
     * @return
     */
    @Override
    public boolean isPhoneExist(String userPhone) {
        boolean isExist = false;
        UserDto user = userDtoMapper.isPhoneExist(userPhone);
        if(user!=null){
            isExist = true;
        }
        return isExist;
    }

    /**
     * 邮箱是否存在
     * @param userEmail
     * @return
     */
    @Override
    public boolean isEmailExist(String userEmail) {
        boolean isExist = false;
        UserDto user = userDtoMapper.isEmailExist(userEmail);
        if(user!=null){
            isExist = true;
        }
        return isExist;
    }

    /**
     * 检验用户的验证方式是否唯一
     * @param userId
     * @return true代表方式不唯一，false唯一
     */
    @Override
    public boolean findUserVerify(Integer userId) {
        UserDto user =userDtoMapper.findUserById(userId);
        System.out.println("user:"+user);
        if(user.getEmail()!=null && user.getPhone()!=null ) {
            return true;
        }
        return false;
    }



    @Override
    public boolean uploadAvatar(String dataURL,String userId) {
        String path = "E:/Springboot/app/src/main/resources/static/asserts/images/avatar";
        //System.out.println(path);
        String imgName=userId+".jpg";
        try{
            ComUtils.decodeBase64DataURLToImageAndUpload(dataURL, path, imgName);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
